<template>
    <van-nav-bar title="订单结算" left-text="返回" left-arrow @click-left="onClickLeft" />

    <van-contact-card type="edit" :tel="address.mobile" :name="address.consignee" @click="onOrder" v-if="JSON.stringify(address) != '{}'" />

    <van-contact-card type="add" @click="onOrder" v-else />
            <van-card
                :price="this.pronum*Cartdata.price"
                :title="Cartdata.name"
                class="goods-card"
            >
                <template #thumb>
                    <img :src="Cartdata.cover_cdn" alt="">
                </template>
                <template #num>
                    <van-stepper v-model="pronum" theme="round" button-size="22" disable-input />
                </template>

                <template #desc>
                    <span>商品单价：￥{{Cartdata.price}}</span>
                </template>
            </van-card>

    <van-cell-group inset>
    <van-field
        v-model="content"
        rows="2"
        autosize
        label="订单备注"
        type="textarea"
        maxlength="50"
        placeholder="请输入订单备注"
        show-word-limit
    />
    </van-cell-group>

    <van-submit-bar :price="this.pronum*Cartdata.price*100" button-text="结算" @submit="onSubmit" />

</template>
<script>
import {showNotify} from 'vant'
export default {
    created() {
        let address = this.$cookies.get('address') ? this.$cookies.get('address') : {}

        this.id = this.$route.query.proid ? this.$route.query.proid : ''

        this.getConfirmData()

        if(JSON.stringify(address) == '{}')
        {
            this.getAddrData()
        }else{
            this.address = address
        }
    },
    data() {
        return {
            address:{},
            LoginUser:this.$cookies.get('LoginAuth'),
            id:0,
            Cartdata:[],
            content:'',
            pronum:1
        }
    },
    methods: {
        onClickLeft()
        {
            this.$router.go(-1)
        },
        onOrder()
        {
            this.$router.push({path:'/user/address/index',query:{action:'order'}})
        },
        async getAddrData()
        {
            let result = await this.$api.AddressOrder({busid:this.LoginUser.id})

            if(result.code === 1)
            {
                this.address = result.data
            }
        },
        async getConfirmData()
        {
            let result = await this.$api.ProductInfo({proid:this.id})
            console.log(result)
            if(result.code === 0)
            {
                showNotify({
                    type:'warning',
                    message:result.msg
                })

                return false
            }

            this.Cartdata = result.data
        },
        async onSubmit()
        {
            // 订单数据
            let data = {
                busid:this.LoginUser.id,
                proid:this.$route.query.proid,
                addrid:this.address.id,
                content:this.content,
                num:this.pronum
            }

            let result = await this.$api.toAdd(data)
                        if(result.code === 1)
            {
                showNotify({
                    type:'success',
                    message:result.msg,
                    onClose:() => {
                        this.$router.push('/user/order/index')
                    }
                })

                return false
            }else{
                showNotify({
                    type:'warning',
                    message:result.msg
                })

                return false
            }
            
        }
    },
    computed:{
        total()
        {
            let total = 0

            if(this.CartList.length > 0)
            {
                for(let item of this.CartList)
                {
                    total += parseFloat(item.total)
                }
            }

            return total * 100
        }
    }
}
</script>